<template>
    <div class="jianzhan-content">
        <Crumb name="Sparepart Request"></Crumb>
        <div class="content">
            <Search>
                <el-input
                        v-model="key"
                        size="small"
                        class="input"
                        placeholder="Search By Request Number"
                        prefix-icon="el-icon-search"
                ></el-input>
                <el-button
                        size="small"
                        type="primary"
                >Search
                </el-button>
            </Search>
            <div class="table-wrapper">
                <el-table
                        :data="purchaseRequestList"
                        border
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="applicationNum"
                            label="Request Number"
                            width="180"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="applicationDateStr"
                            label="Request Date"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="applicant"
                            label="Request By"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="customer"
                            label="Customer"
                            width="250">
                    </el-table-column>

                    <el-table-column
                            prop="applicationStatusDesc"
                            label="Status"
                            width="100"
                    >
                    </el-table-column>
                    <el-table-column label="Opr">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    @click="openEdit(scope.row)">Detail
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="page">
                    <el-pagination background
                                   :page-size="pageSize"
                                   @current-change="changePage"
                                   layout="prev, pager, next"
                                   :total="total"/>
                </div>

            </div>

            <div class="modal-wrapper">
                <el-dialog
                        title="Sparepart Request"
                        :visible.sync="dialogVisible"
                        width="80%"
                        top="0px"
                >
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm"
                             :inline="true">
                        <el-form-item label="Request No." prop="applicationNum">
                            <el-input disabled class="input"  v-model="ruleForm.applicationNum"></el-input>
                        </el-form-item>
                        <el-form-item label="Request Date" prop="applicationDateStr">
                            <el-date-picker disabled class="input" v-model="ruleForm.applicationDateStr"
                                            value-format="yyyy-MM-dd"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="Request By" prop="applicant">
                            <el-input disabled class="input"  v-model="ruleForm.applicant"></el-input>
                        </el-form-item>
                        <el-form-item label="Customer" prop="customer">
                            <el-input disabled class="input"  v-model="ruleForm.customer"></el-input>
                        </el-form-item>

                        <el-form-item label="Approve By" prop="approveBy">
                            <el-input class="input" disabled v-model="ruleForm.approveBy"></el-input>
                        </el-form-item>
                    </el-form>
                    <div class="items">
                        <span>items:</span>

                        <el-button size="small" type="primary" @click="toSpCenter">Check part catalog</el-button>
                        <div class="table">
                            <Data-base @deleteItem="deleteItem" :partList="partList"></Data-base>
                        </div>
                    </div>

                    <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false">Cancel</el-button>
                        <el-button  type="warning" @click="approve(2)">Reject</el-button>
                        <el-button type="primary" @click="approve(3)">Approve</el-button>
                    </span>
                </el-dialog>
            </div>
        </div>
    </div>
</template>

<script>
    import Crumb from '@/components/common/crumb'
    import Search from '@/components/common/search'
    import DataBase from '@/components/part/spDataBase'
    import {mapGetters, mapActions} from 'vuex';
    import {ERROR_OK} from "../../util/config";
    import dbMixins from '../../util/spMixins';
    import mixins from '../../util/mixins'
    import {getQuestList,getQuestDetail,approveQuest} from "../../api/customerRequest";

    export default {
        name: "purchase-approve",
        mixins: [dbMixins, mixins],
        data() {
            return {
                requestList: [],
                partItemList: [],
                ruleForm: {
                    applicationNum:'',
                    applicant:'',
                    applicationDateStr:'',
                    approveBy:'',
                    partsPurchaseApplicationDetails:'',
                    customer:''
                },
                rules: {
                    applicationNum: [
                        {required: true, message: 'Request No is required', trigger: 'blur'},
                    ],
                    applicationDateStr: [
                        {required: true, message: 'Request Date is required', trigger: 'change'}
                    ],
                    applicant: [
                        {required: true, message: 'Request By is required', trigger: 'change'},
                    ],
                    approveBy: [
                        {required: true, message: 'Approved By is required', trigger: 'change'},
                    ],
                    customer: [
                        {required: true, message: 'Name is required', trigger: 'change'}
                    ]
                },
                key: '',
                dialogVisible: false
            }
        },
        computed: {
            purchaseRequestList() {
                return this.requestList.filter(item => item.applicationNum.toLowerCase().indexOf(this.key.toLowerCase()) > -1).slice((this.pageNum - 1) * this.pageSize, this.pageNum * this.pageSize)
            },
            ...mapGetters(['customerList', 'employeeList'])
        },
        beforeRouteEnter(from, to, next) {
            next(vm => {
                vm._getAllList();
                vm.loadCustomerList();
                vm.loadEmployeeList();
            })
        },
        methods: {
            toSpCenter(){
                window.open('http://sp.sh-inline.com')
            },
            async approve(status1){
              const {status,msg} = await approveQuest(this.ruleForm.id,status1);
              if (status === ERROR_OK) {
                  this.$notify.success('check success');
                  this._getAllList();
              }else{
                  this.$notify.error(msg);
              }
            },
            clear() {
                // this.$refs.ruleForm.resetFields();
                this.ruleForm = {
                    applicationNum:'',
                    applicant:'',
                    applicationDateStr:'',
                    approveBy:'',
                    partsPurchaseApplicationDetails:'',
                    customer:'',
                    id:''
                };
                this.partList = []
            },
            async openAdd() {
                this.clear();
                this.dialogVisible = true
            },
            async openEdit(r) {
                let {status, data} = await getQuestDetail(r.id);
                if (status === ERROR_OK) {
                    this.ruleForm = r;
                    this.partList = data.map(item => {
                        return {
                            ...item.spareParts,
                            number: item.partsQuantity,
                            stockQuantity: item.partStock
                        }
                    })

                }
                this.dialogVisible = true

            },
            async _getAllList() {
                let {data, status} = await getQuestList();
                if (status === ERROR_OK) {
                    this.requestList = data
                }
            },
            ...mapActions(['loadCustomerList', 'loadEmployeeList'])
        },
        watch: {
            key(key) {
                this.total = this.requestList.filter(item => item.customer.toLowerCase().indexOf(key.toLowerCase()) > -1).length
            }
        },
        components: {
            Crumb,
            Search,
            DataBase
        }
    }
</script>

<style scoped lang="scss">
    @import "../../css/mixins";

    .content {
        @include content
    }

    .modal-wrapper {
        .input {
            width: 200px;
        }
        .items {
            span {
                display: inline-block;
                width: 140px;
                text-align: right;
                font-size: 16px;
                margin-right: 10px;
            }
            label {
                width: 142px;
                height: 32px;
                display: inline-block;
                text-align: center;
                color: #fff;
                margin-right: 20px;
                cursor: pointer;
                line-height: 32px;
                border-radius: 3px;
                background: #409Eff;
            }
            .table {
                margin-top: 5px;
            }
        }
    }
</style>